<script type="text/javascript" src="java/formval.js"></script>
<script type="text/javascript">
function validateOnSubmit(formElem) {
	var elem;
	var errs=0;
	if (!validatePresent(formElem.KategorieID,  'KategorieError', true))  		errs = 1;
	if (!validatePresent(formElem.Beschreibung,  'BeschreibungError', true))  	errs = 1;
	//if (!validatePresent(formElem.FirmaID,  'FirmaIDError', true))  			errs = 1;
	
	if (errs==0){
		formElem.submit();
	}
	};
function setFileOption(ch){

	if(ch){
		document.getElementById('file').style.display='block';
		document.getElementById('LogoID').style.display='none';
		}else{
		document.getElementById('file').style.display='none';
		document.getElementById('LogoID').style.display='block';
	}
};
</script>
<?php
function GetKategorie(& $KategorieList, $selectID) {
	$sql = "select * FROM kategorie";
	global $_db_link;
	$_rd = mysql_query($sql, $_db_link);
	if ($_rd) {
		while ($daten = mysql_fetch_array($_rd, MYSQL_ASSOC)) {
			$datakategorie[] = $daten;
		}
	}
	if ($selectID == NULL || $selectID == 0) {
		$KategorieList = $KategorieList . '<option value="" selected="selected">Please Select</option>';
	} else {
		$KategorieList = $KategorieList . '<option value="">Please Select</option>';
	}
	$KategorieList = $KategorieList . getOption(& $datakategorie, $selectID);
	$KategorieList = $KategorieList . "</select>";
}

if (!empty ($_SESSION["CMS_User"]["ID"])) {
	if ($_POST) {
		//print_r($_POST);
		if ($_POST["Upload"] == 'on') {
			uploadFile();
		}
		if (!empty ($_POST["AbteilungID"])) { //edit

			if (!empty ($_POST["FirmaID"]) && !empty ($_POST["Beschreibung"])) {
				$AbteilungID = $_POST["AbteilungID"];
				if ($_POST["Upload"] != 'on') {
					$LogoID = $_POST["LogoID"];
				}
				$sql = "update abteilung set 
												FirmaID=" . $_POST["FirmaID"] . "
												,LogoID='" . $LogoID . "'
												,Beschreibung='" . $_POST["Beschreibung"] . "'
												,Aktivitat='" . $_POST["Aktivitat"] . "'
												,VideoLink='" . $_POST["VideoLink"] . "'
												,GalerieID='" . $_POST["GalerieID"] . "'
												where ID=$AbteilungID";
				$_rd = mysql_query($sql, $_db_link);
				$FirmaID = $_POST["FirmaID"];
			}
		} else { //Add
			if (!empty ($_POST["FirmaID"]) && !empty ($_POST["KategorieID"]) && !empty ($_POST["Beschreibung"])) {
				$sql = "insert into 
											     abteilung (FirmaID,KategorieID,LogoID,Beschreibung,Aktivitat,VideoLink,GalerieID,Sortiren,Status) 
												values(" . $_POST["FirmaID"] . "
												," . $_POST["KategorieID"] . "
												,'" . $LogoID . "'
												,'" . $_POST["Beschreibung"] . "'
												,'" . $_POST["Aktivitat"] . "'
												,'" . $_POST["VideoLink"] . "'
												,'" . $_POST["GalerieID"] . "',1,0)";
				//echo $sql;
				$_rd = mysql_query($sql, $_db_link);
				$AbteilungID = mysql_insert_id();
				$FirmaID = $_POST["FirmaID"];
				//echo $sql;
			}
		}
	}

}
if (!empty ($_GET["FirmaID"])) {
	$FirmaID = $_GET["FirmaID"];
}
if (!empty ($_GET["AbteilungID"])) {
	$AbteilungID = $_GET["AbteilungID"];
}
if ($FirmaID != NULL && $FirmaID != 0) {

	if ($AbteilungID) {
		$sql = "select * FROM abteilung WHERE ID=$AbteilungID";
		$_rd = mysql_query($sql, $_db_link);
		if ($_rd) {
			$daten = mysql_fetch_array($_rd, MYSQL_ASSOC);
		}
	}
	//--- Logo List
	$LogoList = "<select name='LogoID' id='LogoID'>";
	getListBox($LogoList, $daten["LogoID"], "files", "WHERE UserID=" . $_SESSION["CMS_User"]["ID"]);
	$LogoList = $LogoList . "</select>";
	//---- Kategorie list 
	$dis = $AbteilungID ? 'DISABLED' : '';
	$KategorieList = '<select name="KategorieID" id="KategorieID" ' . $dis . ' onchange="validatePresent(this, \'KategorieError\');">';
	GetKategorie($KategorieList, $daten["KategorieID"]);
	//---- Galerie List
	$GalerieList = "<select name='GalerieID' id='GalerieID'>";
	getListBox($GalerieList, $daten["GalerieID"], "galerie", "WHERE UserID=" . $_SESSION["CMS_User"]["ID"], "Name");
	$GalerieList = $GalerieList . "</select>";
?>
	<div style="width:100%;background-color:#F1EEF3;margin:0px"> 
	<table class="tableEdit">
	<?PHP if(isset($AbteilungID)){ ?>
		<tr>
		<td class="lableEdit"><?PHP  LangText("ID");?>:</td>
		<td class="inputEdit"><input type="text" name="AbteilungID" id="AbteilungID" READONLY value='<?PHP echo $AbteilungID; ?>' /></td>
		<td class="validateEdit"></td>
		</tr>
		<?PHP } ?>
	<tr>
	<td class="lableEdit"><?PHP  LangText("FirmaID");?>:</td>
	<td class="inputEdit"><input type="text" name="FirmaID" id="FirmaID" READONLY value='<?PHP echo $FirmaID; ?>' /></td>
	<td class="validateEdit"></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("KategorieID");?>:*</td>
	<td class="inputEdit"><?PHP echo $KategorieList; ?></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="KategorieError" value='<?PHP LangText("KategorieError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("LogoID");?>:</td>
	<td class="inputEdit"><input type="checkbox" onchange="setFileOption(this.checked)" id="Upload" name="Upload"/> 
	 <?PHP  LangText("Upload-Datei");?> 
	  <?PHP echo $LogoList; ?> <input type="file" id="file" name="file" style="display:none" /></td>
	<td class="validateEdit"></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Beschreibung");?>:*</td>
	<td class="inputEdit"><textarea name="Beschreibung"  onchange="validatePresent(this, 'BeschreibungError');" id="Beschreibung"><?PHP echo $daten["Beschreibung"]; ?></textarea></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="BeschreibungError" value='<?PHP LangText("BeschreibungError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Aktivitat");?>:</td>
	<td class="inputEdit"><textarea name="Aktivitat" id="Aktivitat"><?PHP echo $daten["Aktivitat"]; ?></textarea></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="AktivitatError" value='<?PHP LangText("AktivitatError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("VideoLink");?>:</td>
	<td class="inputEdit"><input type="text" name="VideoLink" id="VideoLink" value='<?PHP echo $daten["VideoLink"]; ?>' /></td>
	<td class="validateEdit"></td>
	</tr>
	
	<tr>
	<td class="lableEdit"><?PHP  LangText("GalerieID");?>:</td>
	<td class="inputEdit"><?PHP echo $GalerieList; ?></td>
	<td class="validateEdit"></td>
	</tr>
	
	<tr>
	<td class="lableEdit"></td>
	<td class="inputEdit"><input type="button"  onclick="validateOnSubmit(this.form);" class="BootenLink" value='<?PHP if($AbteilungID!=NULL && $AbteilungID != 0 ){LangText("Bearbeiten");}else{ LangText("Himzufugen");}?>' />
	<input type="button"  onclick="window.location='index.php?tag=<?PHP echo $_GET['tag']; ?>&page=FirmaEditPro&FirmaID=<?PHP echo $FirmaID; ?>&MoudeTagID=<?PHP echo $_GET['MoudeTagID']; ?>'" class="BootenLink" value='<?PHP LangText("Abbrechen");?>' /></td>
	<td class="validateEdit"></td>
	</tr>
	</table>
	</div>
	
	<?php


}
?> 